//
//  SelectImageView.m
//  XiaoTianClassmate
//
//  Created by GP on 2019/9/5.
//  Copyright © 2019年 济南高品伟业信息科技有限公司. All rights reserved.
//

#import "SelectImageView.h"

#import "ImageViewCell.h"

#import "YBImageBrowser.h"

static NSString * const reuseIdentifier = @"Cell";


@interface SelectImageView ()<UICollectionViewDelegate,UICollectionViewDataSource,YBImageBrowserDataSource>

@property (nonatomic,weak)UICollectionView *collectionView;

@end

@implementation SelectImageView

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    if (self = [super initWithCoder:aDecoder]) {
        [self initSubView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self initSubView];
    }
    return self;
}

- (void)initSubView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // 设置滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.minimumLineSpacing = 5;
    
    UICollectionView *collect = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [self addSubview:collect];
    [collect mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.bottom.mas_equalTo(0);
        
    }];
    
    collect.delegate = self;
    
    collect.dataSource = self;
    
    _collectionView = collect;
    
    collect.backgroundColor = [UIColor clearColor];
    
    UINib *nib = [UINib nibWithNibName:@"ImageViewCell" bundle:[NSBundle mainBundle]];
    
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:reuseIdentifier];
    
}

// 返回一组有多少个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.images.count;
}

// 返回每个cell的样子
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.imageView.image = self.images[indexPath.row];
    WS(weakSelf);
    cell.deleteItem = ^{
        
        weakSelf.deleteItem(indexPath.row);
    };
    
    return cell;
}

- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(75, 70);
}

- (void)setImages:(NSArray *)images{
    
    _images = images;
    
    [self.collectionView reloadData];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    if (self.selectImage) {
//         NSLog(@"222");
//        self.selectImage(0);
//    }
     //self.selectImage();
   // [self selectedIndex:indexPath.row withImageURLs:self.images];
    
}
- (void)selectedIndex:(NSInteger)index withImageURLs:(NSArray *)urlsArray{
        NSMutableArray *datas = [NSMutableArray array];
        [urlsArray enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            // 网络图片
            YBIBImageData *data = [YBIBImageData new];
            data.imageName = obj;
            //        data.projectiveView = [self viewAtIndex:idx];
            [datas addObject:data];
        }];
        YBImageBrowser *browser = [YBImageBrowser new];
        browser.dataSourceArray = datas;
        browser.currentPage = index;
        [browser show];
    }
     
//       YBImageBrowser *browser = [YBImageBrowser new];
//       browser.dataSource = self;
//          browser.currentPage = indexPath.row;
//       //展示
//       [browser show];

//
//
//}




/**
 返回数据源数量

 @param imageBrowser 图片浏览器
 @return 数量
 */
//- (NSInteger)yb_numberOfCellsInImageBrowser:(YBImageBrowser *)imageBrowser{
//     return self.images.count;
//
//}

//- (id<YBImageBrowserDataSource>)yb_imageBrowserView:(YBImageBrowserView *)imageBrowserView dataForCellAtIndex:(NSUInteger)index {
//    PHAsset *asset = (PHAsset *)self.images[index];
//    if (asset.mediaType == PHAssetMediaTypeVideo) {
//
//        // Type 1 : 系统相册的视频 / Video of system album
//        YBVideoBrowseCellData *data = [YBVideoBrowseCellData new];
//        data.phAsset = asset;
//        data.sourceObject = [self sourceObjAtIdx:index];
//
//        return data;
//    } else if (asset.mediaType == PHAssetMediaTypeImage) {
//
//        // Type 2 : 系统相册的图片 / Image of system album
//        YBImageBrowseCellData *data = [YBImageBrowseCellData new];
//        data.phAsset = asset;
//        data.sourceObject = [self sourceObjAtIdx:index];
//
//        return data;
//    }
//    return nil;
//}
@end
